<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="modalTitle" @ok="onButtonOkClick">
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, unref, inject } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';

  import { dutyScheduleMemberFormSchema } from './dutyschedule.data';
  import { setShiftMember } from '/@/api/duty/dutySchedule';

  import { useMessage } from '/@/hooks/web/useMessage';
  import { OperationTypeEnum } from '/@/enums/appEnum';

  const { createMessage } = useMessage();

  export default defineComponent({
    name: 'EditDutyTeamMember',
    components: { BasicModal, BasicForm },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      //获取定义的表单
      const editDutyScheduleMemberFormSchema = dutyScheduleMemberFormSchema;

      //弹出窗口界面的操作参数
      const op = ref(OperationTypeEnum.UN_KNOWN);

      // const dataId = ref('');
      //传递过来的行数据用于回显
      let memberData = {};

      const moduleName = inject<string>('moduleName');
      //标题--根据传入的模块名称+操作类型组合得到
      const modalTitle = ref('标题');

      const [registerForm, { setFieldsValue, resetFields, validate }] = useForm({
        labelWidth: 100,
        schemas: editDutyScheduleMemberFormSchema,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });

      /**
       * 根据父组件传递过来的参数获取对应的操作，操作不同所对应的界面不同
       */
      const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
        console.log(data);
        //接收父组件传递的数据
        if (data?.op) {
          //判断是否传入op属性
          op.value = data.op;
        }

        //接收传递过来的值班人员信息，回显数据
        if (data?.member) {
          // dataId.value = data.dataId;
          memberData = {
            id: data.member.id,
            memberid: data.member.memberid,
            memberName: data.member.memberName,
            odr: data.member.odr,
            chief: data.member.chief,
          };
          console.log(memberData);
        }

        if (moduleName) {
          modalTitle.value = unref(moduleName);
        }
        if (op.value != OperationTypeEnum.UN_KNOWN && op.value != OperationTypeEnum.CUSTOME) {
          modalTitle.value += '--' + op.value;
        }

        resetFields(); //重置表单
        setModalProps({ confirmLoading: false });

        //设置表单回显
        setFieldsValue({
          ...memberData,
        });
      });

      /**
       * 点击确认按钮，保存修改的数据
       */
      async function onButtonOkClick() {
        const values = await validate();
        //调用接口进行保存
        setShiftMember(values)
          .then((res) => {
            if (!res || Object.keys(res).length == 0) {
              createMessage.success('设置成功。', 2);
            }
            opSuccess();
          })
          .catch((ex) => {
            console.log('catch ', ex);
            createMessage.error(ex.msg);
          });
      }

      //操作成功后
      function opSuccess() {
        closeModal();
        emit('success');
      }

      return {
        op,
        moduleName,
        modalTitle,
        registerModal,
        registerForm,
        onButtonOkClick,
        opSuccess,
      };
    },
  });
</script>
